//
//  HomeReviewCVC.swift
//  placepic
//
//  Created by elesahich on 2020/07/08.
//  Copyright © 2020 elesahich. All rights reserved.
//

import UIKit

class HomeReviewCVC: UICollectionViewCell {
    
    @IBOutlet weak var reviewTextView: UITextView!
    @IBOutlet weak var reviewTitleLabel: UILabel!
    
    var reviewClosure: (() -> ()) = {}
    var editingflag: Bool = false // false: Empty // hasText나 이런거로 대치가능
    
    let textViewAttributes: [NSAttributedString.Key:Any] = [
        .font: UIFont.systemFont(ofSize: 14),
        .foregroundColor: UIColor.gray90,
        .paragraphStyle: {
            let paragraph = NSMutableParagraphStyle()
            paragraph.lineSpacing = 4.5
            return paragraph
        }()
    ]
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        setReviewTextField()
        setupAccessoryView()
    }
}

extension HomeReviewCVC {
    
    private func setReviewTextField() {
        reviewTitleLabel.font = .boldSystemFont(ofSize: 15)
        reviewTitleLabel.textColor = UIColor.gray90
        
        reviewTextView.delegate = self
        reviewTextView.layer.cornerRadius = 5
        reviewTextView.backgroundColor = UIColor.gray10
        reviewTextView.contentInset = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
    }
    
    private func setupAccessoryView() {
        let toolBar = UIToolbar()
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        let btnDoneBar = UIBarButtonItem(title: "Done",
                                         style: .done,
                                         target: self,
                                         action: #selector(doneClicked))
        toolBar.tintColor = UIColor.gray80
        toolBar.sizeToFit()
        toolBar.setItems([flexibleSpace, btnDoneBar], animated: true)
        reviewTextView.inputAccessoryView = toolBar
    }

    @objc func doneClicked() {
        reviewClosure()
        self.endEditing(true)
    }
}

extension HomeReviewCVC: UITextViewDelegate {
        	
    func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
        reviewTextView.resignFirstResponder()
        reviewTextView.layer.borderColor = UIColor.gray10.cgColor
              reviewTextView.layer.borderWidth = 1.5
        return true
    }
    
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        reviewTextView.typingAttributes = textViewAttributes
        reviewTextView.layer.cornerRadius = 10
        reviewTextView.layer.borderColor = UIColor.gray90.cgColor
        reviewTextView.layer.borderWidth = 1.5
        return true
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        endEditing(true)
    }
    
    func textViewDidChange(_ textView: UITextView) {
        if reviewTextView.hasText {
            editingflag = true
            reviewClosure()
            DispatchQueue.main.async {
                NotificationCenter.default.post(name: .homeWriteTextViewisEditingNotification, object: nil, userInfo: ["editingflag" : self.editingflag])
            }
        } else {
            editingflag = false
            DispatchQueue.main.async {
                NotificationCenter.default.post(name: .homeWriteTextViewisEditingNotification, object: nil, userInfo: ["editingflag" : self.editingflag])
            }
        }
    }
}

